package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.woniu.mapper.VideoMapper;
import com.woniu.pojo.Chapter;
import com.woniu.mapper.ChapterMapper;
import com.woniu.service.IChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.vo.ChapterVO;
import com.woniu.vo.VideoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author xiaoruan
 * @since 2023-03-15
 */
@Service
public class ChapterServiceImpl extends ServiceImpl<ChapterMapper, Chapter> implements IChapterService {
    @Resource
    ChapterMapper chapterMapper;
    @Resource
    VideoMapper videoMapper;

    @Override
    public List<ChapterVO> findChaterBycourseID(String courseId) {

//        List<ChapterVO> list =  chapterMapper.findALLChapter(courseId);


        List<ChapterVO> list = chapterMapper.findChaterBycourseId(courseId);

        for (ChapterVO chapterVO : list) {

            List<VideoVO> videoVOList = videoMapper.findVideoByChapterId(chapterVO.getId());

            chapterVO.setVideoVOList(videoVOList);
        }

        return list;
    }

    @Override
    public void deleteById(String id) {

        List<VideoVO> VideoVOList = videoMapper.findVideoByChapterId(id);

        if (VideoVOList == null || VideoVOList.size()==0){
            chapterMapper.deleteById(id);
        }else {
            throw new RuntimeException("该章节下面有视频，不能进行删除操作");
        }

    }

    @Override
    public Integer getMaxSort(String courseId) {
       Integer sort = chapterMapper.getmaxSort(courseId);

        return sort;
    }
}
